<?php


class Entry extends ActiveRecord {

	
	private $entrycode;
	// Inherits $tablename from ActiveRecord
    // Inherits $dbobj from ActiveRecord
	
	public function __construct($entrycode) {
		
		$this->entrycode = $entrycode;
		$this->tablename = 'entries';
	}
	
	
	public function getTitle() {
	
	    preg_match("#<h3(.*?)>(.*?)<a href=(.*?)>(.*?)</a>(.*?)</h3>#", $this->entrycode, $entryhead);
	    if(!empty($entryhead[3]) && !empty($entryhead[4])){
	        $entrytitle = $entryhead[4];
	        $entryurl = str_replace('"', '', str_replace("'", "", $entryhead[3]));
	        return array("url"=>$entryurl, "title"=>$entrytitle);
	    }else{
	        return false;
	    }
	}
	
	
	public function getLabels() {
	
	    preg_match("#post-labels(.*?)>(.*?)<\/span>#", $this->entrycode, $labelsspan);
	    preg_match_all("#<a href=(.*?)>(.*?)<#", $labelsspan[2], $labels);
	
	    if (!empty($labels[2])) {
	    	   
	        return $labels[2];
	    } else {
	    	
	    	return false;
	    }
	}
	
	
	public function getDate($url) {
	
	    $startpos = strpos($url, '/', 7) + 1;
	    $endpos = strpos($url, '/', $startpos + 5);
	    $date = substr($url, $startpos, $endpos - $startpos);
	    if (strlen($date) == 7) {
	    	
	        return $date;
	    } else {
	    	
	        return false;
	    }
	}
	
	
	/**
	 * Extract the entry text from html
	 *
	 * We must try different regexp to find content, blooger blogs can use different templates
	 */
	public function getContent() {
	
	    global $CONFIG;
	
	    $this->entrycode = preg_replace("/\n/", " ", $this->entrycode);
	
	
	    for ($i=0 ; $i<count($CONFIG->entriesregexps) ; $i++) {
	
	        preg_match_all($CONFIG->entriesregexps[$i], $this->entrycode, $content);
	
	        if ($content[1] && $content[1] != '') {
	
	            $html = '';
	            foreach ($content[1] as $paragraph) {
	                $html.= $paragraph;
	            }
	
	            return $html;
	        }
	
	    }
	
	    // If we can't find anything
	    return '';
	}
	
	
    public function getCleanContent($code=false) {
    
        $brs = array('<br />', '<br>', '<br/>');
        $tags = '/<(.*?)>/';
    
        $showcode = str_replace($brs, '||%10||', $code);
        $showcode = str_replace('</p>', '||%10||||%10||', $showcode);
        $showcode = preg_replace($tags, '', $showcode);
        $showcode = str_replace('||%10||', '<br />', $showcode);
    
        return $showcode;
    }

	
	public function saveKeywords($entryid) {
		
	}
	
}


?>
